// import { h, ref } from 'vue';
import { FormSchema } from '/@/components/Table';
import { Checkbox } from 'ant-design-vue';
import { isArray } from '/@/utils/is';
import componentSetting from '/@/settings/componentSetting';
import { getDepartment } from '/@/api/system/system';
import { getAdminFlowListTaskStrategy, getAdminFlowListView } from '/@/api/flowManage/flowManage';
import { WorkflowLinkDb, WorkflowActionDb } from '/@/enums/flowManage';
// import { type } from 'os';
import { useUserStore } from '/@/store/modules/user';
const userStore = useUserStore();
const serverInfo = userStore.getServerInfo;

export const rankList = [
  {
    value: '',
    label: '不限定',
  },
];

export const timeDelayedUnitList = [
  {
    value: '0',
    label: '天',
  },
  {
    value: '1',
    label: '小时',
  },
  {
    value: '2',
    label: '工作日',
  },
  {
    value: '3',
    label: '工作小时',
  },
];

export const actionApplicantSelScopeOptions = [
  {
    value: '0',
    label: '全部',
  },
  {
    value: '1',
    label: '指定成员',
  },
  {
    value: '2',
    label: '指定角色',
  },
];

export const ActionApplicantSelModeOptions = [
  {
    value: '0',
    label: '单选',
  },
  {
    value: '1',
    label: '多选',
  },
];
export interface StrategyList {
  code: string;
  name: string;
  selectable: Boolean;
}
export let strategyList: StrategyList[] = [];

export const formSchema: FormSchema[] = [
  {
    field: 'ActionApproverType',
    label: '审批人类型',
    component: 'RadioButtonGroup',
    colProps: {
      span: 24,
    },
    defaultValue: '1',
    componentProps: ({ formActionType, formModel }) => {
      return {
        options: [
          {
            value: '0',
            label: '默认',
          },
          {
            value: '1',
            label: '发起者选人',
          },
        ],
        onChange: (value, node) => {
          console.log('value', value, node);
          formModel.userName = '';
          formModel.userRealName = '';
          formModel.spanMode = '';
          const { updateSchema } = formActionType;
          if (value == '0') {
            updateSchema({
              field: 'relateRoleToOrganization',
              ifShow: true,
            });
          } else {
            updateSchema({
              field: 'relateRoleToOrganization',
              ifShow: false,
            });
          }
        },
      };
    },
  },
  {
    field: 'ActionApplicantSelMode',
    label: '审批人',
    component: 'RadioButtonGroup',
    colProps: {
      span: 24,
    },
    componentProps: {
      options: ActionApplicantSelModeOptions,
    },
    slot: 'ActionApplicantSelMode',
    ifShow: ({ values }) => values.ActionApproverType == '1',
  },
  {
    field: 'ActionApplicantSelScope',
    label: '人员范围',
    component: 'RadioButtonGroup',
    colProps: {
      span: 24,
    },
    slot: 'ActionApplicantSelScope',
    ifShow: ({ values }) => values.ActionApproverType == '1',
  },
  {
    field: 'handlePerson',
    label: '处理人员',
    component: 'Input',
    colProps: {
      span: 24,
    },
    slot: 'handlePerson',
    ifShow: ({ values }) => values.ActionApproverType == '0' || !values.ActionApproverType,
  },
  {
    field: 'nodeScript',
    label: '脚本选人',
    component: 'InputTextArea',
    colProps: {
      span: 24,
    },
    slot: 'nodeScript',
    show: ({ values }) => values.userName == WorkflowActionDb.PRE_TYPE_NODE_SCRIPT,
  },
  // {
  //   field: 'title',
  //   label: '标题',
  //   component: 'Input',
  //   colProps: {
  //     span: 24,
  //   },
  // },
  {
    field: 'title',
    label: '标题',
    component: 'AutoComplete',
    colProps: {
      span: 24,
    },
    componentProps: {
      showSearch: false,
      options: [
        {
          label: '审批',
          value: '审批',
        },
        {
          label: '审核',
          value: '审核',
        },
        {
          label: '发起',
          value: '发起',
        },
        {
          label: '复核',
          value: '复核',
        },
        {
          label: '拟稿',
          value: '拟稿',
        },
        {
          label: '核稿',
          value: '核稿',
        },
        {
          label: '归档',
          value: '归档',
        },
      ],
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'relateRoleToOrganization',
    label: '角色关联',
    helpMessage: '设为关联后，如果角色中存在有多个用户，系统将自动根据组织机构图及行文方向就近匹配',
    component: 'Checkbox',
    colProps: {
      span: 24,
    },
    ifShow: ({ values }) => values.ActionApproverType == '0' || !values.ActionApproverType,
    // render: ({ model, field }) => {
    //   return model[field];
    // },
  },
  {
    field: 'direction',
    label: '行文方向',
    helpMessage: '上一节点至本节点的行文方向',
    component: 'Select',
    colProps: {
      span: 24,
    },
    defaultValue: '2',
    componentProps: {
      showSearch: false,
      // optionFilterProp: 'label',
      options: [
        {
          label: '上级',
          value: '2',
        },
        {
          label: '先本部门后上级',
          value: '6',
        },
        {
          label: '下级',
          value: '0',
        },
        {
          label: '先本部门后平级',
          value: '1',
        },
        {
          label: '本部门、平级及上级部门',
          value: '5',
        },
        {
          label: '本部门',
          value: '3',
        },
      ],
      getPopupContainer: () => document.body,
    },
    ifShow: ({ values }) => values.relateRoleToOrganization == true,
  },
  {
    label: '关联节点',
    field: 'relateToAction',
    helpMessage: '上一节点至本节点的行文方向',
    component: 'Select',
    colProps: {
      span: 24,
    },
    componentProps: {
      showSearch: false,
      // optionFilterProp: 'label',
      options: [
        {
          label: '关联上一节点',
          value: 'default',
        },
        {
          label: '关联开始节点',
          value: 'starter',
        },
        {
          label: '关联表单中的部门字段',
          value: 'dept',
        },
      ],
      getPopupContainer: () => document.body,
    },
    ifShow: ({ values }) => values.relateRoleToOrganization == true,
  },
  {
    field: 'relateDeptManager',
    label: '分管部门',
    helpMessage: '用户能否分管所关联节点上的人员',
    colProps: {
      span: 24,
    },
    component: 'Checkbox',
    ifShow: ({ values }) => values.relateRoleToOrganization == true,
  },
  {
    field: 'userRealName',
    label: '角色/用户',
    component: 'InputTextArea',
    colProps: {
      span: 24,
    },
    componentProps: {
      disabled: true,
    },
    ifShow: ({ values }) => values.ActionApproverType == '0' || !values.ActionApproverType,
  },
  {
    field: 'spanMode',
    label: '当前为',
    component: 'Input',
    colProps: {
      span: 24,
    },
    render: ({ model, field }) => {
      return model[field];
    },
    ifShow: ({ values }) => values.ActionApproverType == '0' || !values.ActionApproverType,
  },
  {
    field: 'rank',
    label: '用户职级',
    component: 'Select',
    colProps: {
      span: 24,
    },
    show: false,
    componentProps: {
      options: rankList,
    },
  },
  {
    field: 'checkState',
    label: 'checkState',
    component: 'Input',
    colProps: {
      span: 24,
    },
    show: false,
  },
  {
    field: 'fieldWriteText', //fieldWrite
    label: '可写字段',
    component: 'InputTextArea',
    colProps: {
      span: 24,
    },
    componentProps: {
      disabled: true,
    },
    slot: 'fieldWriteText',
  },
  {
    field: 'fieldHideText', //fieldHide
    label: '隐藏字段',
    component: 'InputTextArea',
    helpMessage: '字段被隐藏后，在处理流程时将不可见',
    colProps: {
      span: 24,
    },
    componentProps: {
      disabled: true,
    },
    slot: 'fieldHideText',
  },
  // {
  //   field: 'fieldHideText', //fieldHide
  //   label: '流程抄送',
  //   component: 'InputTextArea',
  //   colProps: {
  //     span: 24,
  //   },
  //   slot: 'fieldHideText',
  // },
  {
    field: 'item1',
    label: '结束节点',
    component: 'RadioButtonGroup',
    helpMessage: '如果是结束节点，则该节点处理后流程变为结束状态',
    colProps: {
      span: 24,
    },
    componentProps: {
      options: [
        {
          value: '1',
          label: '是',
        },
        {
          value: '0',
          label: '否',
        },
      ],
    },
  },
  {
    field: 'trFlag',
    label: '标志位',
    component: 'Checkbox',
    colProps: {
      span: 24,
    },
    slot: 'trFlag',
  },
  {
    label: '水印文字',
    field: 'watermarkText',
    component: 'Input',
    helpMessage: '水印文字',
    colProps: {
      span: 24,
    },
    slot: 'trWatermarkText',
  },
  {
    field: 'gjxz',
    label: '高级选项',
    component: 'Divider',
    colProps: {
      span: 24,
    },
  },
  {
    field: 'strategy',
    label: '分配策略',
    component: 'Select',
    colProps: {
      span: 24,
    },

    componentProps: async ({ formActionType, formModel }) => {
      console.log('formModel', formModel);
      let options = (await getAdminFlowListTaskStrategy()) || [];
      if (!isArray(options)) {
        options = [];
      }
      options.unshift({
        code: '',
        name: '处理者指定',
      });
      const { updateSchema, setFieldsValue } = formActionType;
      if (formModel?.strategy) {
        const stra = formModel.strategy;
        if (stra?.indexOf('x') == 0) {
          options?.forEach((item) => {
            if (item.code === 'x') {
              item.code = formModel.strategy;
            }
          });
          updateSchema({
            field: 'x_num',
            ifShow: true,
          });
          setFieldsValue({ x_num: stra.substring(2) });
        }
      }
      strategyList = options;
      const obj = {
        options,
        defaultValue: '',
        // mode: 'multiple',
        fieldNames: { label: 'name', value: 'code', key: 'code' },
        getPopupContainer: () => document.body,

        onChange: (val) => {
          const fModel: any = options.find((item: any) => item.code == val);
          updateSchema({
            field: 'x_num',
            ifShow: fModel == undefined ? false : fModel?.name?.includes('x'),
          });
        },
      };

      updateSchema([
        {
          field: 'strategy',
          componentProps: obj,
        },
      ]);

      return obj;
    },
  },

  {
    field: 'x_num',
    label: 'x',
    component: 'InputNumber',
    colProps: {
      span: 24,
    },
    defaultValue: 1,
    componentProps: {
      min: 1,
      precision: 0,
    },
    ifShow: false,
  },
  {
    field: 'ignoreType',
    label: '跳过方式',
    component: 'Select',
    helpMessage: '如设为无用户时跳过，则未选择用户时，也将被跳过',
    colProps: {
      span: 24,
    },
    componentProps: {
      showSearch: false,
      options: [
        {
          value: '0',
          label: '无用户时跳过',
        },
        {
          value: '1',
          label: '无用户时不允许跳过',
        },
        {
          value: '2',
          label: '无用户或用户之前处理过则跳过',
        },
        {
          value: '3',
          label: '角色比较大小时不允许跳过',
        },
      ],
    },
  },
  {
    field: 'kind',
    label: '动作',
    component: 'Select',
    colProps: {
      span: 24,
    },
    componentProps: {
      showSearch: false,
      options: [
        {
          value: '0',
          label: '处理',
        },
        {
          value: '1',
          label: '审阅',
        },
        {
          value: '2',
          label: '子流程',
        },
        {
          value: '3',
          label: '自动处理',
        },
      ],
    },
    dynamicRules: ({ values, model }) => {
      return [
        {
          required: true,
          validator: (_, value) => {
            if (value == '2' && values.actionForm !== '') {
              model.actionForm = '';
              return Promise.reject('当设置了节点上的表单时，不能启用子流程');
            }
            return Promise.resolve();
          },
        },
      ];
    },
  },

  // {
  //   label: '退回方式',
  //   field: 'isMobileStart',
  //   component: 'Select',
  //   colProps: {
  //     span: 24,
  //   },
  //   componentProps: {
  //     showSearch: false,
  //     options: [
  //     ],
  //   },
  // },

  {
    label: '分支模式',
    field: 'branchMode',
    component: 'Select',
    helpMessage: '当分支线上无条件时，分支模式才生效',
    colProps: {
      span: 24,
    },
    componentProps: {
      showSearch: false,
      options: [
        {
          value: '1',
          label: '多选',
        },
        {
          value: '0',
          label: '单选',
        },
        {
          value: '2',
          label: '全部',
        },
      ],
    },
  },
  {
    label: '延迟提交',
    field: 'isDelayTr',
    component: 'Select',
    colProps: {
      span: 24,
    },
    slot: 'isDelayTr',
  },
  {
    label: '限定部门',
    field: 'dept', //dept deptName
    component: 'ApiTreeSelect',
    componentProps: {
      api: getDepartment,
      multiple: true,
      maxTagCount: 3,
      resultField: 'list',
      fieldNames: {
        label: 'name',
        key: 'code',
        value: 'code',
      },
      getPopupContainer: () => document.body,
    },
    colProps: {
      span: 24,
    },
  },
  {
    label: '限定部门表单域',
    field: 'deptField',
    component: 'Select',
    helpMessage: '只有限定部门控件内的人员才能处理，当角色关联行文方向时无效',
    colProps: {
      span: 24,
    },
    componentProps: {
      showSearch: false,
      options: [],
    },
  },
  {
    label: '被退回可重选用户',
    field: 'canSelUserWhenReturned', //1
    component: 'Checkbox',
    helpMessage: '被退回后是否可重选用户，不勾选表示只能提交给退回者',
    colProps: {
      span: 24,
    },
  },
  {
    label: '存档',
    field: 'flagSaveArchive', //trArchive
    component: 'Select',
    helpMessage: '只有限定部门控件内的人员才能处理，当角色关联行文方向时无效',
    colProps: {
      span: 24,
    },
    componentProps: {
      showSearch: false,
      options: [
        {
          value: '0',
          label: '不存档',
        },
        {
          value: '2',
          label: '自动存档',
        },
        // {
        //   value:'3',
        //   label:'公文归档'
        // },
      ],
    },
  },
  {
    label: '环节颜色',
    field: 'OfficeColorIndex',
    component: 'Select',
    helpMessage: '在流程列表中显示当前环节的颜色',
    colProps: {
      span: 24,
    },
    slot: 'OfficeColorIndex',
  },
  {
    label: '表单',
    field: 'actionForm',
    component: 'Select',
    helpMessage: '节点上所用的表单',
    colProps: {
      span: 24,
    },
    componentProps: {
      showSearch: true,
      options: [
        {
          value: '',
          label: '默认',
        },
      ],
    },
    dynamicRules: ({ values, model }) => {
      return [
        {
          validator: (_, value) => {
            if (value != '' && values.kind == '2') {
              model.kind = '0';
              return Promise.reject('当设置了节点上的表单时，不能同时启用子流程');
            }
            return Promise.resolve();
          },
        },
      ];
    },
    ifShow: serverInfo.isMultiFormEnabled,
  },
  {
    label: '表单模式',
    field: 'actionFormMode',
    component: 'Select',
    helpMessage: '创建记录表示当流转至该节点时新建一条记录，否则为编辑已有记录',
    defaultValue: '0',
    colProps: {
      span: 24,
    },
    componentProps: {
      showSearch: false,
      optionFilterProp: 'label',
      options: [
        {
          value: '0',
          label: '修改记录',
        },
        {
          value: '1',
          label: '创建记录',
        },
      ],
    },
    ifShow: serverInfo.isMultiFormEnabled,
  },
  {
    label: '表单视图',
    field: 'formView',
    component: 'Select',
    helpMessage: '只有限定部门控件内的人员才能处理，当角色关联行文方向时无效',
    colProps: {
      span: 24,
    },
    componentProps: {
      showSearch: false,
      optionFilterProp: 'label',
      options: [
        {
          value: '0',
          label: '默认',
        },
      ],
    },
  },
  {
    label: '是否提醒',
    field: 'isMsg', //1
    component: 'Checkbox',
    helpMessage: '在本节点提交时是否发送消息提醒下一步处理人员',
    colProps: {
      span: 24,
    },
  },
  {
    label: '提醒人员',
    field: 'imgComb',
    component: 'Input',
    helpMessage: '如果不设置，默认仅提醒下一步处理人员',
    colProps: {
      span: 24,
    },
    slot: 'imgComb',
  },
  {
    label: '模块过滤',
    field: 'isModuleFilter',
    component: 'Checkbox',
    helpMessage: '如果存在用嵌套表格2，是否启用其模块中配置的过滤条件',
    colProps: {
      span: 24,
    },
    show: false,
  },
  {
    label: '保存按钮',
    field: 'isBtnSaveShow', //1
    component: 'Checkbox',
    helpMessage: '保存按钮是否显示',
    colProps: {
      span: 24,
    },
    // suffix: '显示',
    componentProps: {},
    renderComponentContent: '显示',
  },
  {
    label: '提交按钮',
    field: 'btnAgreeName',
    component: 'Input',
    helpMessage: '提交按钮的名称，空则默认为同意',
    colProps: {
      span: 24,
    },
  },
  {
    label: '拒绝按钮',
    field: 'btnRefuseName',
    component: 'Input',
    helpMessage: '拒绝按钮的名称，空则默认为拒绝',
    colProps: {
      span: 24,
    },
  },
  {
    label: '审核人',
    field: 'isShowNextUsers', //1
    component: 'Checkbox',
    helpMessage: '是否显示下一节点上的审核人',
    colProps: {
      span: 24,
    },
    renderComponentContent: '显示下一节点上的审核人',
    componentProps: {},
  },
  {
    label: '流转页面',
    field: 'redirectUrl',
    component: 'Input',
    helpMessage: '交办至下一节点后重定向的页面',
    colProps: {
      span: 24,
    },
  },
  {
    label: '内部名称',
    field: 'internalName',
    component: 'Input',
    helpMessage: '节点的内部名称，用于二次开发',
    colProps: {
      span: 24,
    },
    componentProps: {
      disabled: true,
    },
  },
  {
    label: '子流程类型',
    field: 'subFlowTypeCode',
    component: 'Input',
    colProps: {
      span: 24,
    },
    componentProps: {
      options: [],
    },
    slot: 'subFlowTypeCode',
    ifShow: ({ values }) => values.kind == WorkflowActionDb.KIND_SUB_FLOW,
  },
  {
    label: '字段',
    field: 'trMapField',
    component: 'Input',
    colProps: {
      span: 24,
    },
    componentProps: {
      options: [],
    },
    slot: 'trMapField',
    ifShow: ({ values }) => values.kind == WorkflowActionDb.KIND_SUB_FLOW,
  },
  {
    label: '父 -> 子',
    field: 'p2s',
    component: 'Input',
    colProps: {
      span: 24,
    },
    slot: 'p2s',
    ifShow: ({ values }) => values.kind == WorkflowActionDb.KIND_SUB_FLOW,
  },
  {
    label: '子 -> 父',
    field: 's2P',
    component: 'Input',
    colProps: {
      span: 24,
    },
    slot: 's2P',
    ifShow: ({ values }) => values.kind == WorkflowActionDb.KIND_SUB_FLOW,
  },
  {
    field: 'userName',
    label: 'userName',
    component: 'Input',
    colProps: {
      span: 24,
    },
    show: false,
  },
];

export const formSchemaLine: FormSchema[] = [
  {
    field: 'desc',
    label: '描述',
    component: 'Input',
    colProps: {
      span: 24,
    },
    ifShow: ({ values }) => values.isFlagXorRadiate,
  },
  {
    field: 'expireHour',
    label: '到期',
    component: 'Input',
    helpMessage: '下一节点人员处理的到期时间',
    suffix: '小时(0表示不限时)',
    colProps: {
      span: 24,
    },
  },
  {
    field: 'expireAction',
    label: '超期',
    component: 'Select',
    colProps: {
      span: 24,
    },
    componentProps: {
      getPopupContainer: () => document.body,
      options: [
        {
          value: '',
          label: '等待',
        },
        {
          value: 'next',
          label: '交办至后续节点',
        },
        {
          value: 'starter',
          label: '返回给发起人',
        },
      ],
    },
  },
  {
    field: 'conditionType',
    label: '类型',
    helpMessage: '如仅有1个无条件分支则视为默认条件',
    component: 'Select',
    colProps: {
      span: 24,
    },
    componentProps: {
      getPopupContainer: () => document.body,
      options: [
        {
          value: '-1',
          label: '无条件（或默认条件）',
        },
        {
          value: '',
          label: '根据表单',
        },
        {
          value: 'dept',
          label: '根据上一节点用户所在部门',
        },
        {
          value: 'dept_belong',
          label: '根据上一节点用户所属的部门',
        },
        {
          value: 'role',
          label: '根据上一节点用户角色',
        },
        {
          value: 'comb_cond',
          label: '组合条件',
        },
        {
          value: 'script',
          label: '根据脚本',
        },
        {
          value: '1',
          label: '必须执行',
        },
      ],
    },
    ifShow: ({ values }) => values.isFlagXorRadiate,
  },
  {
    field: 'rawTitle',
    label: '条件',
    component: 'Input',
    colProps: {
      span: 24,
    },
    render: ({ model, field }) => {
      return model[field];
    },
    ifShow: ({ values }) =>
      values.isFlagXorRadiate && values.conditionType === WorkflowLinkDb.COND_TYPE_FORM,
  },
  {
    field: 'condValue',
    label: '表单',
    component: 'Input',
    colProps: {
      span: 24,
    },
    slot: 'condValue',
    ifShow: ({ values }) =>
      values.isFlagXorRadiate && values.conditionType === WorkflowLinkDb.COND_TYPE_FORM,
  },
  {
    field: 'depts',
    label: '部门',
    component: 'ApiTreeSelect',
    componentProps: {
      api: getDepartment,
      multiple: true,
      maxTagCount: 3,
      resultField: 'list',
      fieldNames: {
        label: 'name',
        key: 'code',
        value: 'code',
      },
      getPopupContainer: () => document.body,
    },
    colProps: {
      span: 24,
    },
    ifShow: ({ values }) =>
      values.conditionType === WorkflowLinkDb.COND_TYPE_DEPT ||
      values.conditionType === WorkflowLinkDb.COND_TYPE_DEPT_BELONG,
  },
  {
    field: 'deptNames',
    label: '角色',
    component: 'Input',
    colProps: {
      span: 24,
    },
    slot: 'deptNamesSlot',
    ifShow: ({ values }) => values.conditionType === WorkflowLinkDb.COND_TYPE_ROLE,
  },
  {
    field: 'imgComb',
    label: '组合条件',
    component: 'Input',
    colProps: {
      span: 24,
    },
    slot: 'imgComb',
    ifShow: ({ values }) => values.conditionType === WorkflowLinkDb.COND_TYPE_COMB_COND,
  },
  {
    field: 'nodeScript',
    label: '脚本选人',
    component: 'InputTextArea',
    colProps: {
      span: 24,
    },
    slot: 'nodeScript',
    ifShow: ({ values }) => values.conditionType === WorkflowLinkDb.COND_TYPE_SCRIPT,
  },
  {
    field: 'isFlagXorRadiate',
    label: 'isFlagXorRadiate',
    component: 'Input',
    colProps: {
      span: 24,
    },
    show: false,
  },
];

export const colorList = [
  {
    id: '1',
    color: '#558ede',
    name: '天蓝',
  },
  {
    id: '6',
    color: 'red',
    name: '红色',
  },
  {
    id: '3',
    color: 'Turquoise',
    name: '松石绿',
  },
  {
    id: '4',
    color: '#00ff00',
    name: '绿色',
  },
  {
    id: '5',
    color: 'Pink',
    name: '粉红',
  },
  {
    id: '7',
    color: 'yellow',
    name: '黄色',
  },
  {
    id: '9',
    color: 'DarkBlue',
    name: '深蓝',
  },
  {
    id: '10',
    color: 'Teal',
    name: '蓝绿',
  },
  {
    id: '11',
    color: 'green',
    name: '绿色',
  },
  {
    id: '12',
    color: 'Violet',
    name: '紫色',
  },
  {
    id: '13',
    color: 'DarkRed',
    name: '暗红',
  },
  {
    id: '14',
    color: '#FFCC67',
    name: '土黄',
  },
  {
    id: '15',
    color: '#808080',
    name: '深灰',
  },
  {
    id: '16',
    color: '#C0C0C0',
    name: '灰色',
  },
];
